package com.zhimeng.personal.utils.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.zhimeng.personal.R;
import com.zhimeng.personal.utils.BitmapUtils;
import com.zhimeng.personal.utils.DensityUtils;

/**
 * Created by sunzhimeng on 10/31/17.
 */

public class CardMarkerView extends RelativeLayout {
    private Context context;
    private RelativeLayout backgroundView;
    private ImageView frontVeiw;
    private RoundCornersImageView mAvatar;


    public CardMarkerView(Context context) {
        super(context);
        initView(context);
    }

    public void isMarkerSelected(boolean isSelected) {
        if (isSelected) {
            showAsSelected();
        } else showAsUnSelected();
        postInvalidate();
    }

    private void showAsUnSelected() {
        backgroundView.setSelected(false);
    }

    private void showAsSelected() {
        backgroundView.setSelected(true);
        postInvalidate();
    }


    public CardMarkerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        View.inflate(context, R.layout.card_maker_layout, this);
        backgroundView = (RelativeLayout) findViewById(R.id.rl_container);
        frontVeiw = (ImageView) findViewById(R.id.iv_card_marker);
        mAvatar= (RoundCornersImageView) findViewById(R.id.iv_avatar);
        mAvatar.setRadius(DensityUtils.dp2px(context,8),DensityUtils.dp2px(context,8));
        backgroundView.setSelected(false);
    }


    public void setFrontVeiw(Bitmap resource) {
        frontVeiw.setImageBitmap(modifyImageTofit(resource,frontVeiw));
    }

    private Bitmap modifyImageTofit(Bitmap bitmap,ImageView imageView) {
        int height = imageView.getMeasuredHeight();
        int width = imageView.getMeasuredWidth();
        return BitmapUtils.scaleBitmap(bitmap,40,60);
    }

    public void setAvatar(Bitmap bitmap) {
        mAvatar.setImageBitmap(BitmapUtils.getCircleBitmap(bitmap));
    }
}
